package com.kaoshi.myweb.webtest.java.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class ReadFile {

	public static void main(String[] args) throws IOException {
		String file = "C:\\Users\\Administrator\\Desktop\\temp.txt";

		FileInputStream fis = new FileInputStream(file);
		FileChannel fChannel = fis.getChannel();

		// 如果需要读取文件内容， 需要一次性读取出来，否则，可能会截断中文字符，产生乱码，如果需要读取文件里面的内容，一般不使用nio
		ByteBuffer buffer = ByteBuffer.allocate((int) fChannel.size());

		int read = fChannel.read(buffer);

		buffer.flip();

		byte[] bytes = buffer.array();

		System.out.println(new String(bytes, 0, read));
	}

}
